package com.example.restaurantclient.Adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.restaurantclient.Activity.QueueUpInfoActivity;
import com.example.restaurantclient.Activity.QueueUpListActivity;
import com.example.restaurantclient.Bubble.BadgeView;
import com.example.restaurantclient.Entities.News;
import com.example.restaurantclient.Entities.Restaurant;
import com.example.restaurantclient.R;

import java.util.ArrayList;
import java.util.List;

public class CustomRestaurantAdapter extends BaseAdapter {
    private Context context;
    private List<Restaurant> restaurantsItems = new ArrayList<>();
    private int res;
    private BadgeView bv1;

    public CustomRestaurantAdapter(Context context, List<Restaurant> restaurantsItems, int res) {
        this.context = context;
        this.restaurantsItems = restaurantsItems;
        this.res = res;
    }

    @Override
    public int getCount() {
        if (restaurantsItems != null) {
            return restaurantsItems.size();//获得数据的条数
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (restaurantsItems != null) {
            return restaurantsItems.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        //加载布局文件
        LayoutInflater inflater = LayoutInflater.from(context);
        convertView = inflater.inflate(res, null);
        System.out.println("大小11：//" + restaurantsItems.size());
        //获取控件
        Button btn_queue=convertView.findViewById(R.id.btn_queue_up);
        TextView name = convertView.findViewById(R.id.list_item_house_name);
        TextView distance = convertView.findViewById(R.id.list_item_distance);
        TextView address = convertView.findViewById(R.id.list_item_address);
        TextView tableSum = convertView.findViewById(R.id.list_item_table_sum);
        System.out.println(restaurantsItems.toString());
        System.out.println("position:"+position);
//        imageView.setImageBitmap(newItems.get(position).get);
        name.setText(restaurantsItems.get(position).getName()+"");
        distance.setText(restaurantsItems.get(position).getDistance()+"km");
        address.setText(restaurantsItems.get(position).getAddress());
        tableSum.setText(restaurantsItems.get(position).getMinTable()+restaurantsItems.get(position).getMiddleTable()+restaurantsItems.get(position).getMaxTable()+"");
        btn_queue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //获取sp对象， 参数名"data"表示文件名,MOOD_PRIVATE表示文件操作模式
                SharedPreferences sp = context.getSharedPreferences("data",Context.MODE_PRIVATE);
                SharedPreferences .Editor editor = sp.edit(); 		//获取编辑器

                System.out.println("latitude"+restaurantsItems.get(position).getLatitude()+"longtitude"+restaurantsItems.get(position).getLongtitude());
                editor.putFloat("latitude", restaurantsItems.get(position).getLatitude());
                editor.putFloat("longtitude",restaurantsItems.get(position).getLongtitude());
                editor.commit();									//提交修改
                context.startActivity(new Intent(context, QueueUpInfoActivity.class));
                //
            }
        });


        return convertView;
    }

}
